package cn.tqfeiyang.springplus.warehouse.controller;

import cn.tqfeiyang.springplus.warehouse.service.SupplierService;
import cn.tqfeiyang.springplus.warehouse.request.SupplierDTO;
import cn.tqfeiyang.springplus.warehouse.response.SupplierVO;
import cn.tqfeiyang.springplus.warehouse.request.SupplierSTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.tqfeiyang.springplus.framework.response.ApiResult;
import cn.tqfeiyang.springplus.framework.pagination.PageData;
import cn.tqfeiyang.springplus.framework.validation.WhenCreate;
import cn.tqfeiyang.springplus.framework.validation.WhenUpdate;
import cn.tqfeiyang.springplus.framework.validation.EntityId;
import cn.tqfeiyang.springplus.framework.security.JwtSecurityRequirement;
import cn.tqfeiyang.springplus.framework.logging.LoggingRequirement;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;

/**
 * 供应商前端控制器
 *
 * @author tqfeiyang
 * @since 2024-08-01
 */
@Slf4j
@RestController
@Validated
@RequestMapping("/api/warehouse/supplier")
@Tag(name = "基础/供应商")
public class SupplierController {

    /**
     * 供应商服务
     */
    @Resource
    private SupplierService supplierService;

    /**
     * 查询供应商
     *
     * @param supplierSTO 查询数据
     * @return 供应商列表
     */
    @GetMapping("/query")
    @Operation(summary = "查询供应商")
    @PreAuthorize("hasAnyAuthority('warehouse:supplier:query')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult<PageData<SupplierVO>> selectSupplierPage(SupplierSTO supplierSTO) {
        return ApiResult.success(supplierService.selectSupplierPage(supplierSTO));
    }

    /**
     * 读取供应商
     *
     * @param id 供应商id
     * @return 供应商信息
     */
    @GetMapping("/read")
    @Operation(summary = "读取供应商")
    @PreAuthorize("hasAnyAuthority('warehouse:supplier:read')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult<SupplierVO> selectSupplierInfo(@RequestParam @EntityId Long id) {
        return ApiResult.success(supplierService.selectSupplierInfo(id));
    }

    /**
     * 新建供应商
     *
     * @param supplierDTO 请求数据
     * @return 供应商信息
     */
    @PostMapping("/create")
    @Operation(summary = "新建供应商")
    @PreAuthorize("hasAnyAuthority('warehouse:supplier:create')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult<SupplierVO> createSupplier(@RequestBody @Validated(WhenCreate.class) SupplierDTO supplierDTO) {
        Long id = supplierService.createSupplier(supplierDTO);
        return ApiResult.success(supplierService.selectSupplierInfo(id));
    }

    /**
     * 修改供应商
     *
     * @param id 供应商id
     * @param supplierDTO 请求数据
     * @return 供应商信息
     */
    @PostMapping("/update")
    @Operation(summary = "修改供应商")
    @PreAuthorize("hasAnyAuthority('warehouse:supplier:update')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult<SupplierVO> updateSupplier(@RequestParam @EntityId Long id, @RequestBody @Validated(WhenUpdate.class) SupplierDTO supplierDTO) {
        supplierService.updateSupplier(id, supplierDTO);
        return ApiResult.success(supplierService.selectSupplierInfo(id));
    }

    /**
     * 删除供应商
     *
     * @param id 供应商id
     */
    @PostMapping("/delete")
    @Operation(summary = "删除供应商")
    @PreAuthorize("hasAnyAuthority('warehouse:supplier:delete')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult deleteSupplier(@RequestParam @EntityId Long id) {
        supplierService.deleteSupplier(id);
        return ApiResult.success();
    }
}
